package cn.sll.base.service.impl;

import cn.sll.base.mapper.BaseMapper;
import cn.sll.base.query.BaseQuery;
import cn.sll.base.util.PageList;
import cn.sll.base.service.BaseService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public class BaseServiceImpl<T> implements BaseService<T> {


    @Autowired
    private BaseMapper<T> mapper;

    @Override
    public T findById(Long id) {
        return mapper.findById(id);
    }

    @Override
    public List<T> findAll() {
        return mapper.findAll();
    }

    @Override
    @Transactional
    public void add(T t) {
        mapper.add(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        mapper.update(t);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        mapper.delete(id);
    }

    @Override
    public PageList<T> queryByPage(BaseQuery query) {
        Integer total = mapper.queryByCount(query);
        if (total == 0) {
            return  new PageList<>();
        }
        List<T> rows = mapper.queryByPage(query);
        PageList<T> tPageList = new PageList<>(total,rows);
        return tPageList;
    }

    @Override
    @Transactional
    public void batchDelete(List<Long> ids) {
        mapper.batchDelete(ids);
    }

}
